package com.epam.edu.task03.parser;

import com.epam.edu.task03.dances.*;
import org.apache.log4j.Logger;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Zhanna_Mukanova
 * Date: 9/17/13
 * Time: 6:16 PM
 * To change this template use File | Settings | File Templates.
 */
public class XMLParserStAX implements ParserFactory {
    private Logger logger = Logger.getLogger(XMLParserSAX.class);
    private List<Dance> dances = null;
    private List<Dancer> dancers = null;
    private Dance dance = null;
    private Dancer dancer = null;
    private String tagContent = null;

    public void getDancesDatas() {
        logger.info("________________XMLParserStAX_____________________");
        try {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            InputStream input = new FileInputStream("src/main/resources/dances.xml");
            XMLStreamReader reader = factory.createXMLStreamReader(input);
            while (reader.hasNext()) {
                int event = reader.next();
                switch (event) {
                    case XMLStreamConstants.START_ELEMENT:
                        startElement(reader);
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        characters(reader);
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        endElement(reader);
                        break;
                }
            }
        } catch (XMLStreamException exception) {
            logger.error(exception.getMessage());
        } catch (IOException exception) {
            logger.error(exception.getMessage());
        }
        for (Dance dance : dances)
            logger.info(dance.toString());
    }

    private void characters(XMLStreamReader reader) {
        tagContent = reader.getText().trim();
    }

    private void endElement(XMLStreamReader reader) {
        if (reader.getLocalName().equalsIgnoreCase("dance")) {
            dances.add(dance);
        } else if (reader.getLocalName().equalsIgnoreCase("type")) {
            dance.setType(DanceType.fromValue(tagContent));
        } else if (reader.getLocalName().equalsIgnoreCase("scene")) {
            dance.setScene(SceneType.fromValue(tagContent));
        } else if (reader.getLocalName().equalsIgnoreCase("numberOfDancers")) {
            dance.setNumberOfDancers(NumberOfDancersType.fromValue(tagContent));
        } else if (reader.getLocalName().equalsIgnoreCase("music")) {
            dance.setMusic(MusicType.fromValue(tagContent));
        } else if (reader.getLocalName().equalsIgnoreCase("number")) {
            dance.setNumber(Integer.valueOf(tagContent));
        } else if (reader.getLocalName().equalsIgnoreCase("dancers")) {
            dance.setDancers(dancers);
        } else if (reader.getLocalName().equalsIgnoreCase("dancer")) {
            dancers.add(dancer);
        } else if (reader.getLocalName().equalsIgnoreCase("dancerName")) {
            dancer.setDancerName(tagContent);
        } else if (reader.getLocalName().equalsIgnoreCase("numberOfYears")) {
            dancer.setNumberOfYears(Integer.valueOf(tagContent));
        }
    }

    private void startElement(XMLStreamReader reader) {
        String str = reader.getLocalName();
        if (reader.getLocalName().equalsIgnoreCase("dances")) {
            dances = new LinkedList<Dance>();
        } else if (reader.getLocalName().equalsIgnoreCase("dance")) {
            dance = new Dance();
            dance.setDanceName(reader.getAttributeValue(null, "danceName"));
        } else if (reader.getLocalName().equalsIgnoreCase("dancers")) {
            dancers = new LinkedList<Dancer>();
        } else if (reader.getLocalName().equalsIgnoreCase("dancer")) {
            dancer = new Dancer();
        }
    }
}
